<?php

namespace BXTR\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use BXTR\UserBundle\Form\Type\UserRegistrationType;
use BXTR\UserBundle\Entity\User;
use BXTR\UserBundle\Entity\UserRole;
use Symfony\Component\HttpFoundation\Request;

/** 
 * Auth Controller.
 *
 * Manage authentification processes (login and logout).
 */
class AuthController extends Controller
{
    /** 
     * login action
     *
     * Display a login page.
     */
    public function loginAction(Request $request)
    {
        $session = $request->getSession();
        
        // Fetch authentification errors
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }
        
        $user = new User();
        $form = $this->createFormBuilder($user)
            ->setAction($this->generateUrl('_security_check'))
            ->setMethod('POST')
            // The username and password fields are displayed in the view 
            // as they have specific names
            ->add('login', 'submit')
            ->getForm();

        //$form->handleRequest($request);
        //$factory = $this->get('security.encoder_factory');
//        if ($form->isValid()) {
            // Redirect the user to the symfony's login page
            
  //          return $this->redirect($this->generateUrl('_security_check'));
            /*$user = $registerForm->getData();
            //var_dump($registerForm->getData());
            $encoder = $factory->getEncoder($user);
            $encodedPassword = $encoder->encodePassword($user->getPassword(), $user->getSalt());
            $user->setPassword($encodedPassword);
            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();
            return $this->redirect($this->generateUrl('bxtr_signup_success'));*/
    //    }

        return $this->render('BXTRUserBundle:Auth:Login.html.twig', array(
            'last_username' => $session->get(SecurityContext::LAST_USERNAME),
            'error'         => $error,
            //'register_form' => $registerForm->createView(),
            'form' => $form->createView()
        ));
    }

    public function signupAction()
    {

        //$registerForm = $this->createForm(new RegisterFormType(), $user);
        $user = new User();
        $builder = $this->createFormBuilder()
            ->add('username')
            ->add('password', 'repeated', Array(
                'type' => 'password'
            ))
            ->add('Form.Register', 'submit')
            ->getForm();

        //$registerForm->handleRequest($request);
        $form = $this->createForm($builder, $user);

        return $this->render('BXTRUserBundle:Auth:Signup', Array(
            'form' => $form->createView()
        ));
    }

    public function signupSuccessAction()
    {
        return $this->render('BXTRUserBundle:Signup:Success.html.twig');
    }

    public function addAdminAction($name, $password)
    {
        $factory = $this->get('security.encoder_factory');
        $user = new User();

        $encoder = $factory->getEncoder($user);
        $user->setUsername($name);
        $user->setMail('random@email.net');
        $user->setDateSubscribe(new \DateTime());
        $role = new UserRole();
        $role->setName('ROLE_ADMIN');
        $user->addDbRole($role);
        $encodedPassword = $encoder->encodePassword($password, $user->getSalt());

        $user->setPassword($encodedPassword);
        
        $em = $this->getDoctrine()->getManager();
        $em->persist($role);
        $em->persist($user);
        $em->flush();

        return $this->redirect($this->generateUrl('bxtr_signup_success'));
    }
}

